package com.itheima.jobs;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.RedisConstant;
import com.itheima.service.SetmealService;
import com.itheima.utils.QiniuUtils;
import org.springframework.beans.factory.annotation.Autowired;
import redis.clients.jedis.JedisPool;

import java.util.List;
import java.util.Set;

public class ClearImgJob {

    @Autowired
    private JedisPool jedisPool;

    @Reference
    private SetmealService setmealService;
    //定时清理垃圾图片
    public void clearImg() {

        //比较两个集合的集合差,生成一个新的集合
        Set<String> sdiff = jedisPool.getResource().sdiff(RedisConstant.SETMEAL_PIC_RESOURCES, RedisConstant.SETMEAL_PIC_DB_RESOURCES);
        if (sdiff != null) {
            for (String fileName : sdiff) {
                //删除七牛云对应的图片
                QiniuUtils.deleteFileFromQiniu(fileName);
                //删除redis对应的图片
                jedisPool.getResource().srem(RedisConstant.SETMEAL_PIC_RESOURCES, fileName);
            }
        }
    }
}
